<template>
  <!-- 分销钱包-钱包管理 -->
  <div class="component-distribution-distributionUserWallet">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distributionMsg.distriTelPhone') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distr-user-wallet-basic-table">
        <el-table
          ref="distrUserWalletBasicTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <!-- 分销员昵称 -->
          <el-table-column
            prop="nickName"
            :label="$t('distribUserWallet.distributorNickname')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.nickName : '' }}
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser ? scope.row.distributionUser.userMobile : '' }}
            </template>
          </el-table-column>
          <!--          状态-->
          <el-table-column
            prop="state"
            :label="$t('distribUserWallet.status')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === -1"
                class="tag-text"
              >
                {{ $t("distribUserWallet.perBan") }}
              </div>
              <div
                v-if="scope.row.state === 0"
                class="tag-text"
              >
                {{ $t("product.pendingReview") }}
              </div>
              <div
                v-if="scope.row.state === 1"
                class="tag-text"
              >
                {{ $t("publics.normal") }}
              </div>
              <div
                v-if="scope.row.state === 2"
                class="tag-text"
              >
                {{ $t("distribUserWallet.ban") }}
              </div>
              <div
                v-if="scope.row.state === 3"
                class="tag-text"
              >
                {{ $t("publics.unPassed") }}
              </div>
            </template>
          </el-table-column>

          <!-- 待结算金额 -->
          <el-table-column
            prop="unsettledAmount"
            :label="$t('distribUserWallet.waitTosettled')"
          >
            <template #default="scope">
              {{ scope.row.unsettledAmount }}
            </template>
          </el-table-column>
          <!-- 可提现金额 -->
          <el-table-column
            prop="settledAmount"
            :label="$t('distribUserWallet.withdrawalAmount')"
          >
            <template #default="scope">
              {{ scope.row.settledAmount }}
            </template>
          </el-table-column>
          <!-- 已失效金额 -->
          <el-table-column
            prop="invalidAmount"
            :label="$t('distribUserWallet.invalidAmount')"
          >
            <template #default="scope">
              {{ scope.row.invalidAmount }}
            </template>
          </el-table-column>
          <!-- 积累收益 -->
          <el-table-column
            prop="addupAmount"
            :label="$t('distribution.accumulateIncome')"
          >
            <template #default="scope">
              {{ scope.row.addupAmount }}
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column
            v-if="paySettlementType!==1"
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="130"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === 1"
                class="text-btn-con"
              >
                <div
                  v-if="isAuth('admin:distributionUserWallet:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row)"
                >
                  {{ $t('crud.updateBtn') }}
                </div>
                <div
                  v-if="isAuth('distribution:distributionUserWallet:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.walletId)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddOrUpdate from './distribution-userWallet-update.vue'

const allinpayStore = useAllinpayStore()
const paySettlementType = computed(() => {
  return allinpayStore.paySettlementType
})

onMounted(() => {
  getDataList()
})
let tempSearchForm = null // 保存上次点击查询的请求条件
const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const dataListLoading = ref(false)
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/distribution/distributionUserWallet/pageAndUser'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam == null ? page.currentPage : pageParam.currentPage,
      size: pageParam == null ? page.pageSize : pageParam.pageSize
    }, tempSearchForm))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    dataListLoading.value = false
  })
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param val
 */
const onAddOrUpdate = (val) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(val)
  })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('scoreProdList.isDelete'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/distribution/distributionUserWallet/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  page.currentPage = 1
  getDataList(page)
}
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

</script>
